package com.lepan.ovgms.manages;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lepan.ovgms.R;
import com.sch.rfview.AnimRFRecyclerView;
import com.sch.rfview.decoration.DividerItemDecoration;
import com.sch.rfview.manager.AnimRFLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pc on 2016/5/5.
 */
public abstract class BaseFragment extends Fragment {
    protected AnimRFRecyclerView mRecyclerView;
    protected View itemApply;
    protected List<Object> datas;
    protected Handler mHandler = new Handler();
    protected MyAdapter mMyAdapter;

    public BaseFragment() {
        // addData();
        datas = new ArrayList<>();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (mRecyclerView == null) {
            // 自定义的RecyclerView, 也可以在布局文件中正常使用
            mRecyclerView = new AnimRFRecyclerView(getActivity());
            // 头部
            View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.header_view, null);
            // 脚部
            View footerView = LayoutInflater.from(getActivity()).inflate(R.layout.footer_view, null);

            // 使用重写后的线性布局管理器
            AnimRFLinearLayoutManager manager = new AnimRFLinearLayoutManager(getActivity());
            mRecyclerView.setLayoutManager(manager);
            mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), manager.getOrientation(), true));
            // 设置刷新动画的颜色
            mRecyclerView.setColor(Color.RED, Color.BLUE);
            // 设置头部恢复动画的执行时间，默认500毫秒
            mRecyclerView.setHeaderImageDurationMillis(300);
            // 设置拉伸到最高时头部的透明度，默认0.5f
            mRecyclerView.setHeaderImageMinAlpha(0.6f);
            // 设置适配器

            setMyAdapter();

            // 设置刷新和加载更多数据的监听，分别在onRefresh()和onLoadMore()方法中执行刷新和加载更多操作
            mRecyclerView.setLoadDataListener(new AnimRFRecyclerView.LoadDataListener() {
                @Override
                public void onRefresh() {
                    new Thread(new MyRunnable(true)).start();
                }

                @Override
                public void onLoadMore() {
                    new Thread(new MyRunnable(false)).start();
                }
            });

            // 刷新
            mRecyclerView.setRefresh(true);

        }

        return mRecyclerView;
    }

    public abstract void setMyAdapter();

    public void refreshComplate() {
        Log.e("XXXX", "refreshComplate before");
        mRecyclerView.getAdapter().notifyDataSetChanged();
        Log.e("XXXX", "refreshComplate after");
    }

    public void loadMoreComplate() {
        Log.e("XXXX", "loadMoreComplate before");
        mRecyclerView.getAdapter().notifyDataSetChanged();
        Log.e("XXXX", "loadMoreComplate after");
    }

    public void setDatas() {

    }

    class MyRunnable implements Runnable {

        boolean isRefresh;

        public MyRunnable(boolean isRefresh) {
            this.isRefresh = isRefresh;
        }

        @Override
        public void run() {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (isRefresh) {
                        setDatas();
                        refreshComplate();
                        // 刷新完成后调用，必须在UI线程中
                        mRecyclerView.refreshComplate();
                    } else {
                        setDatas();
                        loadMoreComplate();
                        // 加载更多完成后调用，必须在UI线程中
                        mRecyclerView.loadMoreComplate();
                    }
                }
            }, 2000);
        }
    }
}
